package com.cr.crandroid.ui.person;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import androidx.navigation.Navigation;

import com.bumptech.glide.Glide;
import com.cr.crandroid.R;
import com.cr.crandroid.base.BaseFragment2;
import com.cr.crandroid.bean.User;
import com.cr.crandroid.ui.person.kefu.KefuFragment;
import com.cr.crandroid.ui.person.user.LoginViewModel;
import com.cr.crandroid.utils.CommonConstant;
import com.cr.crandroid.utils.OConvertUtils;
import com.cr.crandroid.vo.CarVo;
import com.google.gson.Gson;

import de.hdodenhof.circleimageview.CircleImageView;


public class PersonFragment extends BaseFragment2 {


    private boolean isLogin;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_person, container, false);
        CircleImageView circleImageView = root.findViewById(R.id.circleImageView);
        circleImageView.setOnClickListener(this::click);
        TextView textView = root.findViewById(R.id.textView);
        textView.setOnClickListener(this::click);

        // 审核发布
        LinearLayout linearLayoutApprove = root.findViewById(R.id.linearLayoutApprove);
        linearLayoutApprove.setVisibility(View.GONE);
        if (LoginViewModel.user.has("id")) {
            textView.setText(OConvertUtils.getString(LoginViewModel.user.get("username").toString()));
//            if (OConvertUtils.getString(LoginViewModel.user.get("sex").toString()).equals(CommonConstant.FEMALE)) {
//                circleImageView.setImageResource(R.drawable.head_female);
//            }
            // 加载图片
            if (LoginViewModel.user.get("avatar") != null) {
                Glide.with(getContext()).load(OConvertUtils.getString(LoginViewModel.user.get("avatar").toString())).into(circleImageView);
            }else{
                circleImageView.setImageResource(R.drawable.default_head);
            }
            if (OConvertUtils.getString(LoginViewModel.user.get("role").toString()).equals(CommonConstant.ROLE_CODE_ADMIN))
                linearLayoutApprove.setVisibility(View.VISIBLE);
            isLogin = true;
        } else {
            textView.setText("点击登录");
            isLogin = false;
        }
        Bundle arguments = getArguments();
        if (arguments != null && arguments.containsKey("action")) {}
        linearLayoutApprove.setOnClickListener(view -> {
            navigationFragment(R.id.action_navigation_person_to_approveFragment, false);
        });

        // 我的车辆
        LinearLayout linearLayoutCar = root.findViewById(R.id.linearLayoutCar);
        linearLayoutCar.setOnClickListener(view -> {
            navigationFragment(R.id.action_navigation_person_to_carFragment, false);
        });

        // 发布车辆
        LinearLayout linearLayoutPublish = root.findViewById(R.id.linearLayoutPublish);
        linearLayoutPublish.setOnClickListener(view -> {
            navigationFragment(R.id.action_navigation_person_to_publishFragment, false);
        });

        // 智能客服
        LinearLayout linearLayoutKefu = root.findViewById(R.id.linearLayoutKefu);
        linearLayoutKefu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 获取宿主 Activity 的引用
                Activity activity = getActivity();
                if(activity!=null){
                    // 使用宿主 Activity 的上下文来创建 Intent
                    Intent intent=new Intent(activity,KefuFragment.class);
                    // 启动新的 Activity
                    activity.startActivity(intent);
                }
            }
        });
//        linearLayoutKefu.setOnClickListener(view -> {
//            navigationFragment(R.id.action_navigation_person_to_kefuFragment, false);
//        });

        return root;
    }


    private void navigationFragment(int navigateAction, boolean isBack) {
        if (isBack) {
            if (LoginViewModel.user.has("id")) {
                Navigation.findNavController(getView()).navigate(navigateAction);
            } else {
                Navigation.findNavController(getView()).navigate(R.id.action_navigation_person_to_loginFragment);
            }
        } else {
            if (LoginViewModel.user.has("id")) {
                Navigation.findNavController(getView()).navigate(navigateAction);
            } else {
                Navigation.findNavController(getView()).navigate(R.id.action_navigation_person_to_loginFragment, getBundle(navigateAction));
            }
        }

    }

    private Bundle getBundle(int navigateAction) {
        Bundle bundle = new Bundle();
        bundle.putString("jump", String.valueOf(navigateAction));
        return bundle;
    }

    private void click(View view) {
        if (isLogin) {
            Navigation.findNavController(view).navigate(R.id.action_navigation_person_to_infoFragment);
        } else {
            Navigation.findNavController(view).navigate(R.id.action_navigation_person_to_loginFragment);
        }
    }

}